#include "Page.hpp"

using namespace std;

Page::Page()
{
	seed = 0;
}

void Page::generate()
{
	Random r( seed );

	GLenum errCode = glGetError();
	if (errCode != GL_NO_ERROR)
	{
		string errString = (const char*)gluErrorString(errCode);
        Logger::write( "Page", "generate()", errString.c_str(), Logger::Error );
	}
	
	
	for( int x=-1; x<2; x+=2 )
	{
//		Street* s = new Street();
//		s->setPosition( x*50.0, 0.3, 0);
//		s->setXAligned( false );
//		proceduralElements.push_front( s );
		
		for( int y=-1; y<1; y+=2 )
		{
//			Street* s = new Street();
//			s->setPosition( 0, 0.3, y*50.0 );
//			s->setXAligned( true );
//			proceduralElements.push_front( s );
			
			
			Highrise* b = new Highrise();
			b->setGenerationRectangle( x * 50.0f, (x+1)*50.0f, y*50.0f, (y+1)*50.0f );
			b->generate( r );
			proceduralElements.push_front( b );
		}
	}
}

void Page::draw()
{
	assert( glGetError() == GL_NO_ERROR);
	std::list<Element*>::iterator cur = proceduralElements.begin();
	std::list<Element*>::iterator end = proceduralElements.end();
	for( ; cur != end; ++cur )
	{
		(*cur)->draw();
	}
}

void Page::drawShadow()
{
	assert( glGetError() == GL_NO_ERROR);
	std::list<Element*>::iterator cur = proceduralElements.begin();
	std::list<Element*>::iterator end = proceduralElements.end();
	for( ; cur != end; ++cur )
	{
		(*cur)->drawShadow();
	}
}



void Page::drawSimple()
{
	assert( glGetError() == GL_NO_ERROR);
	std::list<Element*>::iterator cur = proceduralElements.begin();
	std::list<Element*>::iterator end = proceduralElements.end();
	for( ; cur != end; ++cur )
	{
		(*cur)->drawSimple();
	}
}

void Page::drawLightVolumes()
{
	std::list<DeferredLight*>::iterator cur = lights.begin();
	std::list<DeferredLight*>::iterator end = lights.end();
	for( ; cur != end; ++cur )
	{
		(*cur)->drawVolume();
	}
}

